﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;


public partial class _News : System.Web.UI.Page
{
    protected LocalFunctions FUNC;
    private int iTotalPages = 0;
    private int iPageSize = 5;
    public int CurrentPage = 1;
    private CalendarPageType _pageType;
    private string sCurrentMonth = string.Empty;
    private DateTime date;
    protected string sKeywords, sDescription;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Rondo)Master).FUNC;
        //sendto.FUNC = FUNC;
        //Calendar1.FUNC = FUNC;

        GetParameters();
        GetAllEvents();
        //lnkEvents.Attributes.Add("style", (FUNC.LangID == 1) ? FUNC.sSelected : FUNC.sSelectedHeb);
        Title = FUNC.GetPhrase(19);
      

        Dal.arParams.Clear();
        Dal.arParams.Add(11); // News
        Dal.arParams.Add(FUNC.LangID);
        DataTable DTtitle = Dal.ExecuteDBsp("RD_SelectStaticPageById", Dal.arParams);
        if (DTtitle != null)
        {
            if (DTtitle.Rows.Count > 0)
            {
                Title = DTtitle.Rows[0]["BrowserTitle"].ToString();
                sKeywords = DTtitle.Rows[0]["Keywords"].ToString();
                sDescription = DTtitle.Rows[0]["Description"].ToString();
                //imgTitle.Src = FUNC.FullAppPath + "/Gallery/" + DTtitle.Rows[0]["imageLink"].ToString();
            }
        }
        lblEmpty.Text=FUNC.GetPhrase(25);

        //Style for hebrew
        if (FUNC.LangID == 2)
        {
            dlEvents.Attributes.Add("style", "float:right;clear:both;margin-right:20px;");
        }
    }

    private void GetParameters()
    {
        _pageType = (CalendarPageType)Convert.ToInt32(Request.QueryString["type"]);
        CurrentPage = Convert.ToInt32(Request.QueryString["page"]);
        long lDate=(long)Convert.ToInt64(Request.QueryString["date"]);
        date = DateTime.FromBinary(lDate);
    }

    protected void dlNews_DataBind(object sender, DataListItemEventArgs e)
    {
          if (FUNC.LangID == 2)
                ((HtmlTable)e.Item.FindControl("tblContent")).Attributes.Add("dir", "rtl");
    }

    private void GetAllEvents()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID);
        Dal.arParams.Add(CurrentPage);
        Dal.arParams.Add(iPageSize);

        DataTable DT = Dal.ExecuteDBsp("RD_GetNews", Dal.arParams);
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                lblEmpty.Visible= false;
                iTotalPages = (int)Math.Ceiling((double)Convert.ToInt32(DT.Rows[0]["TotalRows"]) / iPageSize);
                dlEvents.DataSource = DT;
                dlEvents.DataBind();
                if (iTotalPages > 1)
                {
                    SimplePager();
                }

            }
    }

    public void SimplePager()
    {
        if (CurrentPage < 0) CurrentPage = 1;
        if (iTotalPages < 0) iTotalPages = 0;

        var pager = new PagerBuilder(_pageType);

        if (CurrentPage > 1)
        {
            pager.AddPage("&lt;&lt;", 1);
            pager.AddPage("&lt;", CurrentPage - 1);
        }

        var start = Math.Max(CurrentPage - 2, 1);
        var end = Math.Min(iTotalPages, CurrentPage + 2);

        for (var i = start; i <= end; i++)
        {
            if (i == CurrentPage)
                pager.AddPage(i.ToString(), i, "font-weight: bold; color: White;");
            else
                pager.AddPage(i.ToString(), i);
        }

        if (CurrentPage < iTotalPages)
        {
            pager.AddPage("&gt;", CurrentPage + 1);
            pager.AddPage("&gt;&gt;", iTotalPages);
        }

        divPager.InnerHtml += pager.ToString();
    }

    public class PagerBuilder
    {
        private class PagerLink
        {
            public string Title { get; set; }
            public int PageNo { get; set; }
            public string Class { get; set; }
        }

        private readonly CalendarPageType _urlTemplate;
        private readonly List<PagerLink> _pagerLinks =
                new List<PagerLink>();

        public PagerBuilder(CalendarPageType urlTemplate)
        {
            _urlTemplate = urlTemplate;
        }

        public string PagerClass { get; set; }

        public void AddPage(string title, int pageNo)
        {
            AddPage(title, pageNo, string.Empty);
        }

        public void AddPage(string title, int pageNo,
                            string itemClass)
        {
            var link = new PagerLink
            {
                PageNo = pageNo,
                Title = title,
                Class = itemClass
            };
            _pagerLinks.Add(link);
        }

        public override string ToString()
        {
            var builder = new StringBuilder();

            foreach (var link in _pagerLinks)
            {
                builder.Append("<a");
                if (link.Class != string.Empty)
                {
                    builder.AppendFormat(" style=\"{0}\"", link.Class);
                }
                builder.Append(" href=\"");
                builder.AppendFormat("news.aspx?page={0}&type={1}", link.PageNo, (int)_urlTemplate);
                builder.Append("\">");
                builder.Append(link.Title);
                builder.Append("</a>&nbsp;");
            }
            return builder.ToString();
        }
    }
}
